<?php

// [ 项目评估 ]

namespace app\common\lib;
use think\Db;
use app\common\lib\Cpimg;
use app\common\lib\Cpdf;

class Cp
{
    public static function run($projectId)
    {
        // 项目
        $project = Db::name('project')->where('id',$projectId)->find();

        // 运营数据
        $project['data'] = Db::name('project_data')->where('project_id',$projectId)->find();

        // 合成评估报告图片
        $imgArr = Cpimg::run($project);

        // 生成评估报告
        $res  = Cpdf::run($imgArr);

        // 销毁素材图片
        foreach ($imgArr as $key => $val) @unlink($val);

        if (!$res['result']) return '';

        $cpUri = $res['uri'];

        // 写入历史记录
        $data['project_id']  = $projectId;
        $data['cp_uri']      = $cpUri;
        $data['create_time'] = time();
        Db::name('project_cp_history')->insert($data);

        // 更新项目状态
        Db::name('project')->where('id',$projectId)->update(['is_cp'=>1,'cp_uri'=>$cpUri]);

        return $cpUri;
    }
}